Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
what is the equivalent function to `history()` for calculating indicators outside `handle_data`?

Hi everyone,

I want to calculate indicators for my own data. Inside handle_data we can use history() which seems to be essential for making many indicators.

If I want to calculate indicators before running handle_data or I want to calc my indicators inside pre_func, as we cannot use history() outside handle_data, then what can we use to replace the use of history()?

Thanks

6 responses

Hi Kenny,

Unfortunately, there is currently no alternative to history() outside of handle_data(). If I understand what you're trying to do, it sounds like you should be able to make the call to history() in handle_data(). Is there a reason that it has to be done before handle_data() is called? You can always use schedule_function() to setup a function that calculates your indicator at the start of each day!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Jamie,

Thanks for your recommendations of tutorials, and I have watched and read them all. They are helpful, thanks.

I am struggling with using history(), because I want to use history() to calculate indicators based on my imported data not symbols of quantopian database.

However, history() seems only work with price , open_price, close_price, high, low, and volume of only quantopian database, not at all imported data.

so, beside the soon-to-be depreciated batch_transform or pd.rolling_mean, is there still a way to use history() to calculate indicators for imported data?

Thanks a lot!

Hi Kenny,

I believe Alisa's response to your thread here is relevant to this answer as well.

https://www.quantopian.com/posts/function-like-history-but-for-fetch-csv-data

This is what I use to get the history of data which has been brought in through fetch_csv. Some of that code is specific to the VIX CSV, but hopefully you can get the gist - inside our post_func we CSV-encode the history of the field(s) we care about into a huge string and then in our handle_data we parse it out back into a pandas.Series.

I am using this code in live-trading; it has the benefit that it removes any lookahead-bias which might otherwise be introduced by fetch_csv and simulation is consistent with live-trading.

Hope this helps.

Simon.

Thanks a lot Simon, at the moment I still got a lot to catch up in order to fully understand your codes. but I will keep study and digest your codes.

Good luck. It is a pretty important feature for me, but perhaps less so for Quantopian, so I'm happy to have a workaround that works for my algos.